package com.example.userdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description
 * @Author liqinglin
 * @Date 2024/1/11 17:14
 */
@Configuration
public class ThreadPoolConfig {
    @Bean(name = "initializeExecutor")
    public ThreadPoolTaskExecutor initializeMysqlDataOfTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        executor.setCorePoolSize(10);
        //设置最大线程数
        executor.setMaxPoolSize(50);
        //设置等待队列长度
        executor.setQueueCapacity(1000);
        //线程存活时间:s
        executor.setKeepAliveSeconds(300);
        executor.setThreadNamePrefix("initialize--");
        //设置拒绝策略：调用主线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
